<?php
class admin_template_content_article_listing extends admin_theme
{

	protected function head()
	{
	?>
<script type="text/javascript" language="javascript" src="apps/content/templates/article/js/listing.js"></script>
	<?php
	}

	protected function center()
	{
		$articles = $this->get('articles');
		$categories = $this->get('categories');

		$ui_listing = bone::get_admin_ui('listing');
		$ui_listing->can('create');
		$ui_listing->can('edit');
		$ui_listing->can('unblock');
		$ui_listing->can('block');
		$ui_listing->can('delete');

		$category_options = array();
		$category_options['-1'] = '所有文章';
		$category_options['0'] = '未分类文章';
		foreach ($categories as $category)
        {
            if ($category->level>0)
                $category_options[$category->id] = str_repeat('&nbsp; ', $category->level) . $category->name;
            else
                $category_options[$category->id] = $category->name;
        }

		$ui_listing->set_filter(
            array(
            	'type'=>'text', 
            	'name'=>'key', 
            	'label'=>'关健字', 
            	'value'=>$this->get('key'), 
            	'width'=>'120px'
            ), 
            array(
            	'type'=>'select', 
                'name'=>'category_id', 
            	'label'=>'所属分类', 
            	'options'=>$category_options,
                'value'=>$this->get('category_id')
            ), 
            array(
            	'type'=>'select', 
                'name'=>'status', 
            	'label'=>'状态', 
            	'options'=>array(
                    '-1'=>'所有',
            		'0'=>'公开',
                    '1'=>'屏蔽'
                ),
                'value'=>$this->get('status')
            )
		);

	    $index_categories = array();
		$index_categories[0] = '未分类';
		foreach ($categories as $category)
        {
            $index_categories[$category->id] = $category->name;
        }
	    foreach($articles as $article)
		{
		    $article->formatted_title = limit($article->title, 50);
			$article->category_name = $index_categories[$article->category_id];
			$article->create_time =	date('Y-m-d H:i',$article->create_time);
			$article->creator =	bone::get_user($article->create_by_id)->name;
		}
		
		$ui_listing->set_data($articles);
		
		$ui_listing->set_fields(
			array(
    			'name'=>'id',
    			'label'=>'ID',
    			'align'=>'center',
				'width'=>'30',
			    'order_by'=>'id'
			),
    		array(
    			'name'=>'title',
    			'label'=>'标题',
    			'align'=>'left',
    			'template'=>'<a href="'.BONE_URL.'/?action=content.article.detail&id={id}" title="{title}" target="_blank">{formatted_title}</a>'
    		),
    		array(
    			'name'=>'category_name',
    			'label'=>'所属分类',
    			'align'=>'center',
    		    'width'=>'120'
    		),
    		array(
    			'name'=>'creator',
    			'label'=>'作者',
    			'align'=>'center',
    		    'width'=>'120'
    		),
    		array(
    			'name'=>'create_time',
    			'label'=>'发布时间',
    			'align'=>'center',
    		    'width'=>'120',
    		    'order_by'=>'create_time'
    		),
    		array(
    			'name'=>'hits',
    			'label'=>'点击量',
    			'align'=>'center',
    		    'width'=>'60',
    		    'order_by'=>'hits'
    		),
    		array(
    			'label'=>'设为首页',
    			'align'=>'center',
    		    'width'=>'60',
    		    'template'=>'<a href="javascript:;" onclick="javascript:setHome({id})" class="icon status-{home}" id="home-{id}"></a>'
    		)
		);

		$ui_listing->set_pagination( $this->get('pagination') );
		$ui_listing->order_by($this->get('order_by'), $this->get('order_by_dir'));
		$ui_listing->display();
		
	}	

}
?>